package controller;

import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.table.TableColumnModel;

import model.FileTableModel;

import view.MainFrame;

public class TableHeaderSorter  extends MouseAdapter {
	 
	public TableHeaderSorter(MainFrame frame) {
		this.frame = frame;		
	}
		
	private MainFrame frame;
	
	public static Boolean t = false;
	
	public void mouseClicked(MouseEvent mouseEvent) {
		TableColumnModel columnModel = frame.getMainPanel().getPrimary().getDiskContent().getTable().getColumnModel();
	  
	    int viewColumn = columnModel.getColumnIndexAtX(mouseEvent.getX());
	    int column = frame.getMainPanel().getPrimary().getDiskContent().getTable().convertColumnIndexToModel(viewColumn);
	    if (mouseEvent.getClickCount() == 1 && column != -1) {
	    
	      @SuppressWarnings("unused")
		  int shiftPressed = (mouseEvent.getModifiers() & InputEvent.SHIFT_MASK);
	      boolean ascending = t;
	      if (t == false ) {
	    	  t = true;
	      }else {
	    	  t = false;
	      }
	      
	      if (column == 0) {
	    	  
	    	  ((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).sortByFileNames(ascending);		
	      }
	      else if (column == 1) {
	    	
	    	  ((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).sortByExtensions(ascending) ;
	      }
	      else if (column == 2) {
	    	
	    	  ((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).sortByLastModyfication(ascending);
	      }
	      else if (column == 3) {
	    	
	    	  ((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).sortBySize(ascending);
	      }
	      
	      ((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).fireTableDataChanged();
			
	      
	     
	    }
	 }
	
}
